package edu.cmu.ece.ds.droidchat.model;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class ChatDataSQLHelper extends SQLiteOpenHelper {
	
	private static final String DATABASE_NAME = "droidchat.db";
	private static final int DATABASE_VERSION = 1;
	
	// Table name
	public String TABLE = "";

	// Columns
	public static final String SENDER = "sender"; // sender username
	public static final String MESSAGE = "msg";   // the message content 
	
	
	public ChatDataSQLHelper(Context context, String chatSession) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.TABLE = chatSession;
		Log.e("ChatDataSQLHelper", "ChatDataSQLHelper(): " + this.TABLE);
	}


	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table " + TABLE + "( " + BaseColumns._ID
		+ " integer primary key autoincrement, " + SENDER + " text not null, "
		+ MESSAGE + " text not null);";
		Log.e("ChatData", "onCreate: " + sql);
		db.execSQL(sql);
	}


	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion >= newVersion)
			return;
		
		String sql = null;
		
		if (oldVersion == 1) 
			sql = "alter table " + TABLE + " add note text;";
		if (oldVersion == 2)
			sql = "";

		Log.d("ChatData", "onUpgrade: " + sql);
		if (sql != null)
			db.execSQL(sql);
	}
}
